package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.upmp.domain.IcEmvInfo;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.IcPubKeyInfo;
import cc.rengu.redp.bizimpl.upmp.domain.IcPubKeyInfoBean;
import cc.rengu.redp.bizimpl.upmp.service.IcPubKeyInfoService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:IC卡公钥参数管理")
@RestController
@RequestMapping("/admin/merParam/icPubKeyInfo")
public class IcPubKeyInfoController extends BaseController {
    @Autowired
    private IcPubKeyInfoService icPubKeyInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, IcPubKeyInfo entity) {
        return getDataTable(this.icPubKeyInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/view")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public IcPubKeyInfo getOne(@NotBlank(message = "{required}") String id) {
        IcPubKeyInfo entity = icPubKeyInfoService.getById(id);
        String val = entity.getParaVal();
        int start = Integer.valueOf(val.substring(4, 6)) * 2 + 12;
        int end = Integer.valueOf(val.substring(4, 6)) * 2 + 14;
        String rid = entity.getParaId().trim();
        String gysy = val.substring(start, end);
        String time = val.substring(end + 6, end + 22);
        String yxq = "";
        for (int i = 0; i < time.length(); i++) {
            if (i % 2 != 0) {
                yxq += time.charAt(i);
            }
        }
        String hxsfbs = val.substring(end + 28, end + 30);
        String gysfbs = val.substring(end + 36, end + 38);
        Integer len = (Integer.parseInt(val.substring(end + 44, end + 46), 16)) * 2;
        String gym = val.substring(end + 46, end + 46 + len);
        String gyzs = val.substring(end + 46 + len + 6, end + 46 + len + 8);
        Integer len1 = (Integer.parseInt(val.substring(end + len + 58, end + len + 60), 16)) * 2;
        String gyxyz = val.substring(end + len + 60, end + len + 60 + len1);
        entity.setRid(entity.getParaVal());
        entity.setTgzbs(rid);
        entity.setGysy(gysy);
        entity.setHxsfbs(hxsfbs);
        entity.setGysfbs(gysfbs);
        entity.setGym(gym);
        entity.setGyzs(gyzs);
        entity.setGyxyz(gyxyz);
        entity.setGxyxq(yxq);
        return entity;
    }

    @Log("编辑详情")
    @GetMapping(value = "/viewNew")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public IcPubKeyInfo editShowNew(@NotBlank(message = "{required}") String id)  throws RedpException {
        IcPubKeyInfo entity = icPubKeyInfoService.getById(id);
        String paraVal = entity.getParaVal();
        String rid = paraVal.trim();
        IcPubKeyInfoBean icpubkeyinfobean = JSON.parseObject(paraVal, IcPubKeyInfoBean.class);
        entity.setRid(entity.getParaVal());
        entity.setTgzbs(rid);
        entity.setGyxyz(icpubkeyinfobean.getCHECK_VALUE());
        entity.setHxsfbs(icpubkeyinfobean.getHSID());
        entity.setGysfbs(icpubkeyinfobean.getPKID());
        entity.setGym(icpubkeyinfobean.getPK_MOD());
        entity.setGyzs(icpubkeyinfobean.getPK_EXP());
        entity.setGxyxq(icpubkeyinfobean.getEFFC_DATE());
        return entity;
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "paraId", value = "AID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "paraIdx", value = "参数索引", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "hxsfbs", value = "认证中心哈希算法标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "gysfbs", value = "认证中心公钥算法标识", required = true, dataType = "String")
    })
    public void add(@Valid IcPubKeyInfo entity) throws RedpException {
        try {
            this.icPubKeyInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid IcPubKeyInfo entity) throws RedpException {
        try {
            this.icPubKeyInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("新增")
    @PostMapping("newedit")
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "paraId", value = "AID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "paraIdx", value = "参数索引", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "hxsfbs", value = "认证中心哈希算法标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "gysfbs", value = "认证中心公钥算法标识", required = true, dataType = "String")
    })
    public void newadd(@Valid IcPubKeyInfo entity) throws RedpException {
        try {

           /* User user = RedpUtil.getCurrentUser();
            String crtOrgId = user.getOrgCode();
            if (user.isAdmin()) {
                if (StringUtils.isEmpty(entity.getInstId())) {
                    log.error("保存数据时请先选择法人行!");
                    throw new RedpException("保存数据时请先选择法人行！");
                }
            }*/
            this.icPubKeyInfoService.newCreate(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("newedit")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void newedit(@Valid IcPubKeyInfo entity) throws RedpException {
        try {
                 /* User user = RedpUtil.getCurrentUser();
            String crtOrgId = user.getOrgCode();
            if (user.isAdmin()) {
                if (StringUtils.isEmpty(entity.getInstId())) {
                    log.error("保存数据时请先选择法人行!");
                    throw new RedpException("保存数据时请先选择法人行！");
                }
            }*/
            this.icPubKeyInfoService.newUpdate(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.icPubKeyInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }



    @Log("新增IC卡公钥参数")
    @PostMapping("addIcPubKey")
    public void addIcPubKey(IcPubKeyInfo icEmvInfo) throws RedpException{
        try {
            this.icPubKeyInfoService.addIcPubKey(icEmvInfo);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改IC卡公钥参数")
    @PutMapping("updIcPubKey")
    public void updIcPubKey(IcPubKeyInfo icEmvInfo) throws RedpException{
        try {
            this.icPubKeyInfoService.updIcPubKey(icEmvInfo);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }
    }


}
